<template>
	<div class="app-container">
		<div class="filter-container">
			<div class="fr">
      	<el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
      </div>
    </div>
		<el-table class="tqd_table" :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
      style="width: 100%;">
      <!-- <el-table-column
      align="center"
      type="selection"
      width="55">
    	</el-table-column> -->
			<el-table-column width="60" prop="id" label="序号" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="名称" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.placeName}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        	<router-link :to="'/sys/match/schedule/list?matchId='+scope.row.matchId+'&matchPlaceId='+scope.row.id">
          <el-button type="primary" size="mini">场次管理</el-button>
          </router-link>
          <!-- <el-button size="mini" type="success" @click="handleModifyStatus(scope.row,'draft')">修改</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[15,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div> -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="550px">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="100px" style='width: 100%; '>
        <el-form-item label="场地名称" prop="placeName">
          <el-input v-model="temp.placeName"></el-input>
        </el-form-item>
        <!-- <el-form-item label="记录台数理" prop="name">
          <el-input v-model.Number="temp.name" type="number"></el-input>
        </el-form-item> -->
        <!-- <el-form-item prop="refereeMode" label="裁判制式">
					<el-select class="filter-item" v-model="temp.refereeMode" placeholder="请选择">
            <el-option value="3" label='三裁'>三裁</el-option>
            <el-option value="5" label='五裁'>五裁</el-option>
            <el-option value="7" label='七裁'>七裁</el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
        <el-button v-else type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
	</div>

</template>
<script>
import { getMatchPlaceList, saveMatchPlace } from '@/api/matchPlace'
import waves from '@/directive/waves' // 水波纹指令
import { showMsg } from '@/utils'
export default {
  name: 'complexTable',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        matchId: null
        // pageNum: 1,
        // pageSize: 15
      },
      temp: {
        // id: undefined,
        matchId: null,
        placeName: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        placeName: [{ required: true, message: '必填', trigger: 'blur' }],
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        time: [{ required: true, message: '必选', trigger: 'change' }],
        dataURL: [{ required: true, message: '必填', trigger: 'blur' }],
        fieldNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        refereeMode: [{ required: true, message: '必选', trigger: 'change' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.listQuery.matchId = this.$route.query.id

    this.getList()
  },
  methods: {

    getList() {
      this.listLoading = true
      console.log(this.listQuery)
      getMatchPlaceList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        // id: undefined,
        matchId: null,
        placeName: null
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.matchId = Number(this.$route.query.id)
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          saveMatchPlace(this.temp).then(res => {
            if (res.data.code === 0) {
              this.dialogFormVisible = false
              showMsg(res.data.message, 1)
              this.getList()
            } else {
              showMsg(res.data.message, 2)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          // updateArticle(tempData).then(() => {
          //   for (const v of this.list) {
          //     if (v.id === this.temp.id) {
          //       const index = this.list.indexOf(v)
          //       this.list.splice(index, 1, this.temp)
          //       break
          //     }
          //   }
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: '成功',
          //     message: '更新成功',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },
    handleDelete(row) {
      this.$prompt('请输入密码', '确认删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
        // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        // inputErrorMessage: '邮箱格式不正确'
      }).then(({ value }) => {
        this.$message({
          type: 'success',
          message: '你的密码是: ' + value
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    }
  }
}
</script>